package com.htudata.other.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.htudata.common.enums.RequestSeqEnum;
import com.htudata.common.service.DataEhcacheService;
import com.htudata.convention.domain.ResponseResult;
import com.htudata.core.util.ResultGenerator;
import com.htudata.core.util.operation.StringOperationUtils;
import com.htudata.other.dao.OtherDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class OtherShowService {

  @Autowired
  private DataEhcacheService dataEhcacheService;
  
  @Autowired
  private OtherDao otherDao;
  
  @Value("${bss_info_img_base_url}")
  private String bss_info_img_base_url;
  
  public String getBssInfoList(boolean needJudgeChange) throws Exception {
    ResponseResult responseResult = null;
    try {
      responseResult = ResultGenerator.genResult(RequestSeqEnum.SEQ_001.getCode(),
          selectBssInfoJA(), "获取业务系统信息列表成功");
    } catch (Exception e) {
       throw e;
    } 
    String result = JSON.toJSONString(responseResult);
    if (!needJudgeChange)
      return result; 
    Object temp = this.dataEhcacheService.getValueFromCache(RequestSeqEnum.SEQ_001.getCode());
    if (temp != null) {
      String oldJson = (String)temp;
      if (oldJson.equals(result))
        return null; 
    } 
    this.dataEhcacheService.putValueToCache(RequestSeqEnum.SEQ_001.getCode(), result);
    return result;
  }
  
  public JSONArray selectBssInfoJA() {
    JSONArray result = this.otherDao.selectBssInfoJA();
    JSONObject jo = null;
    for (Object obj : result) {
      jo = (JSONObject)obj;
      jo.put("img", getBssInfoImgTgtUrl(jo.get("img") + ""));
    } 
    return result;
  }
  
  private String getBssInfoImgTgtUrl(String imgName) {
    if (!StringOperationUtils.isValidStr2(imgName))
      return ""; 
    String result = this.bss_info_img_base_url + "/" + imgName;
    return result;
  }
}
